package com.zx.reader.controller;

import com.zx.reader.entity.Evaluation;
import com.zx.reader.entity.Member;
import com.zx.reader.entity.MemberReadState;
import com.zx.reader.service.EvaluationService;
import com.zx.reader.service.MemberService;
import com.zx.reader.utils.ResponseUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api/member")
public class MemberController {
    @Resource
    private MemberService memberService;
    @Resource
    private EvaluationService evaluationService;

    /**
     *
     * @param username
     * @param password
     * @param nickname
     * @param vc
     * @param request
     * @return
     */
    @PostMapping("/registe")
    public ResponseUtils registe(String username, String password , String nickname , String vc , HttpServletRequest request){
        String verifyCode = (String)request.getSession().getAttribute("kaptchaVerifyCode");
        ResponseUtils resp ;
        if(vc == null || verifyCode == null || !vc.equalsIgnoreCase(verifyCode)){
            resp = new ResponseUtils("VerifyCodeError", "验证码错误");
        }else{
            resp = new ResponseUtils();
            //验证码比对成功后进行用户注册
            try {
                Member member = memberService.createMember(username, password, nickname);
                resp = new ResponseUtils();
            }catch (Exception e){
                e.printStackTrace();
                resp = new ResponseUtils(e.getClass().getSimpleName(), e.getMessage());
            }
        }
        return resp;
    }

    /**
     *
     * @param username
     * @param password
     * @param vc
     * @param request
     * @return
     */
    @PostMapping("/check_login")
    public ResponseUtils checkLogin(String username , String password, String vc , HttpServletRequest request){
        String verifyCode = (String)request.getSession().getAttribute("kaptchaVerifyCode");
        ResponseUtils resp ;
        if(vc == null || verifyCode == null || !vc.equalsIgnoreCase(verifyCode)){
            resp = new ResponseUtils("VerifyCodeError", "验证码错误");
        }else{
            resp = new ResponseUtils();
            //验证码比对成功后进行用户登录查询
            try {
                Member member = memberService.checkLogin(username, password);
                member.setPassword(null);
                member.setSalt(null);
                resp = new ResponseUtils().put("member" , member);
            }catch (Exception e){
                e.printStackTrace();
                resp = new ResponseUtils(e.getClass().getSimpleName(), e.getMessage());
            }
        }
        return resp;
    }

    /**
     *
     * @param memberId
     * @return
     */
    @GetMapping("/select_by_id")
    public ResponseUtils selectById(Long memberId){
        ResponseUtils resp = null;
        try {
            Member member = memberService.selectById(memberId);
            resp = new ResponseUtils().put("member", member);
        }catch(Exception ex){
            ex.printStackTrace();
            resp = new ResponseUtils(ex.getClass().getSimpleName(), ex.getMessage());
        }
        return resp;
    }

    /**
     *
     * @param memberId
     * @param bookId
     * @return
     */
    @GetMapping("/select_read_state")
    public ResponseUtils selectMemberReadState(long memberId , Long bookId){
        ResponseUtils resp = null;
        try {
            MemberReadState memberReadState = memberService.selectMemberReadState(memberId, bookId);
            resp = new ResponseUtils().put("readState", memberReadState);
        }catch (Exception e){
            e.printStackTrace();
            resp = new ResponseUtils(e.getClass().getSimpleName(), e.getMessage());
        }
        return resp;
    }

    /**
     *
     * @param memberId
     * @param bookId
     * @param readState
     * @return
     */
    @PostMapping("/update_read_state")
    public ResponseUtils updateReadState(Long memberId , Long bookId , Integer readState){
        ResponseUtils resp = null;
        try {
            MemberReadState memberReadState = memberService.updateMemberReadState(memberId, bookId, readState);
            resp = new ResponseUtils().put("readState" , memberReadState);
        }catch (Exception e){
            e.printStackTrace();
            resp = new ResponseUtils(e.getClass().getSimpleName(), e.getMessage());
        }
        return resp;
    }

    /**
     *
     * @param memberId
     * @param bookId
     * @param score
     * @param content
     * @return
     */
    @PostMapping("/evaluate")
    public ResponseUtils evaluate(Long memberId , Long bookId , Integer score , String content){
        ResponseUtils resp = null;
        try {
            Evaluation evaluation = evaluationService.evaluate(memberId, bookId, score, content);
            resp = new ResponseUtils().put("evaluation" , evaluation);
        }catch (Exception e){
            e.printStackTrace();
            resp = new ResponseUtils(e.getClass().getSimpleName(), e.getMessage());
        }
        return resp;
    }

    /**
     *
     * @param evaluationId
     * @return
     */
    @PostMapping("/enjoy")
    public ResponseUtils enjoy(Long evaluationId){
        ResponseUtils resp = null;
        try {
            Evaluation eva = evaluationService.enjoy(evaluationId);
            resp = new ResponseUtils().put("evaluation" , eva);
        }catch (Exception e){
            e.printStackTrace();
            resp = new ResponseUtils(e.getClass().getSimpleName(), e.getMessage());
        }
        return resp;

    }
}

